require.config({
    baseUrl: baseUrl
});

define(function(require) {
    var $ = require('jquery');
    window.$ = window.jQuery = $;
    var dialog = require('bDialog/dialog');
    window.dialog=dialog;
    var c = require('../common');
    $(function() {
        c.init();

        //减
        $('.numbox .reduce').click(function(){
            var t=$(this);
            var ipt=t.siblings('.tb');
            var v=ipt.val();
            var n=v;
            v=parseInt(v);
            v=(v-1)<0?0:(v-1);

            var product_id = t.closest('div').data('id');
            var cid = t.closest('div').data('cid');
            var tag = t.closest('div').data('tag');
            del(product_id,cid,tag,v,t,ipt);
        });

        //删除购物车商品
        function del(product_id,cid,tag,v,t,ipt){
            var shop_id = $('input[name="shop_id"]').val();
            if(empty(product_id) || empty(cid) || empty(shop_id)){
                c.error('参数错误!',2000);
                return false;
            }
            c.hide();
            var forms = {};
            forms['id'] = product_id;
            forms['cid'] = cid;
            forms['sid'] = shop_id;
            forms['tag'] = tag;
            if(empty(v) || v == 0){
                dialog({title:'温馨提示',height:100,width:500,content:'<div class="tc">是否确定放弃该商品吗？</div>',ok:function(){
                    $.post(URL+"/del",forms,function(data){
                        if(data.status == 1){
                            c.success(data.info,1000);
                            if(empty(data.data['num']) || data.data['num'] == 0){
                                location.href = URL+"/cart?id="+shop_id;
                            }else{
                                if(empty(v)){
                                    t.parents('li').remove();
                                }else{
                                    t.closest('div').data('tag',data.data['tag']);
                                }
                            }
                            ipt.val(v);
                            cart(data);
                            return true;
                        }else{
                            c.error(data.info,2000);
                            return false;
                        }
                    })
                },cancel:function(){},okVal:'确定',cancelVal:'取消'});
            }else{
                $.post(URL+"/del",forms,function(data){
                    if(data.status == 1){
                        c.success(data.info,1000);
                        if(empty(data.data['num']) || data.data['num'] == 0){
                            location.href = URL+"/cart?id="+shop_id;
                        }else{
                            if(empty(v)){
                                t.parents('li').remove();
                            }else{
                                t.closest('div').data('tag',data.data['tag']);
                            }
                        }
                        ipt.val(v);
                        cart(data);
                        return true;
                    }else{
                        c.error(data.info,2000);
                        return false;
                    }
                });
            }
        }

        //加
        $('.numbox .add').click(function(){
            var t=$(this);
            var ipt=t.siblings('.tb');
            var v=ipt.val();
            v=parseInt(v);
            v=v+1;
            ipt.val(v);

            var attr_key = t.closest('div').data('key');
            var product_id = t.closest('div').data('id');
            var cid = t.closest('div').data('cid');
            add(product_id,cid,attr_key,t);
        });

        //增加
        function add(product_id,cid,key,t){
            var shop_id = $('input[name="shop_id"]').val();
            if(empty(product_id) || empty(cid) || empty(shop_id)){
                c.error('参数错误!',2000);
                return false;
            }
            var forms = {};
            forms['id'] = product_id;
            forms['cid'] = cid;
            forms['sid'] = shop_id;
            forms['attr'] = key;
            $.post(URL+"/add",forms,function(data){
                if(data.data['tag']){
                    var tag = t.closest('div').data('tag');
                    t.closest('div').data('tag',tag+','+ data.data['tag']);
                }
                cart(data);
            });
        }

        function cart(data){
            var price=0;
            var num=0;
            $('.goodslist li').each(function(){
                var t=$(this);
                var p=parseFloat(t.find('.unit_price').html());
                var qty=parseInt(t.find('.tb').val());
                price+=qty*p;
                num+=qty;
            });
            var way=$('.theway a.active').data('way');
            if(way=='storedeliver'){
                var fee=parseFloat($('.disfee').html());
            }else{
                var fee=0;
            }

            price = data.data['price'];
            $('.totalprice').html(price);
            $('.payamount').html(price+fee);
            changeBtn();
        }

        //改变按钮状态
        function changeBtn(){
            var totalprice = $('.totalprice').html();
            var sprice = $('input[name="min_send_price"]').val();
            if(parseFloat(totalprice) >= parseFloat(sprice)){
                $('.priceBtn').removeClass('btn-disabled').attr('id','settlement').html('去结算');
            }else{
                $('.priceBtn').addClass('btn-disabled').removeAttr('id').html('满'+parseInt(sprice)+"起送");
            }
        }

        //运送方式选择
        $('.theway a').click(function(event) {
            var t=$(this);
            t.addClass('active').siblings('a').removeClass('active');
            var way=t.data('way');
            if(way=='storedeliver'){
                $('.deliverfree').addClass('hide');
                $('.deliverfee').removeClass('hide');
                var fee=parseFloat($('.disfee').html());
            }else{
                $('.deliverfree').removeClass('hide');
                $('.deliverfee').addClass('hide');
                var fee=0;
            }
            var price=parseFloat($('.totalprice').html());
            $('.payamount').html(price+fee);
        });

        //结算
        $('body').delegate('#settlement','click',function(){
            var remark = $.trim($('textarea[name="remark"]').val());
            var type = $('.theway .active').data('status');
            var price = $('.payamount').html();
            var sid = $('input[name="shop_id"]').val();
            var aid = $('input[name="address_id"]').val();
            var dprice = $('.disfee').html();

            if(empty(type)){
                c.error('请选择配送类型!',2000);
                return false;
            }
            if(empty(price)){
                c.error('应付金额不能为空!',2000);
                return false;
            }
            if(empty(sid)){
                c.error('分店不能为空!',2000);
                return false;
            }
            if(empty(aid)){
                c.error('请选择收货地址!',2000);
                return false;
            }

            var forms = {};
            forms['type'] = type;
            forms['remark'] = remark;
            forms['price'] = price;
            forms['sid'] = sid;
            forms['aid'] = aid;
            forms['dprice'] = dprice;

            $.post(URL+"/settlement",forms,function(data){
                if(data.status == 1){
                    location.href = URL+'/settlement?sid='+sid;
                    return true;
                }else{
                    c.error(data.info,2000);
                    return false;
                }
            });
        });

        //优惠券选择
        $('.selectCoupon').click(function(){
            $(this).find('input[type="radio"]').prop('checked',true);
            $(this).addClass('checked').siblings('li').removeClass('checked');
            var selVal = $('input[name="coupons"]:checked').val();
            var shop_id = $('input[name="shop_id"]').val();
            if(selVal){
                location.href = URL+"/settlement?cid="+selVal+"&sid="+shop_id;
            }
        });

        //选择支付方式
        $('.thewaylist select').change(function(){
            var payway= $('#payway').val();
            if(payway == 2){
                $('.payType').hide();
            }else{
                $('.payType').show();
            }
        });

        //提交订单
        $('#buildOrder').click(function(){
            var maskbg = $('<div class="maskbg"></div>');
            c.wait('正在提交订单...');
            $('body').append(maskbg);

            var sid = $('input[name="sid"]').val();
            var id = $('input[name="id"]').val();
            var price = $('.payamount').html();
            var payway= $('#payway').val();
            if(empty(sid)){
                c.hide();
                $('.maskbg').remove();
                c.error('请选择操作项!',2000);
                return false;
            }
            if(empty(price)){
                c.hide();
                $('.maskbg').remove();
                c.error('价格错误!',2000);
                return false;
            }
            var forms = {};
            forms['sid'] = sid;
            forms['id'] = id;
            forms['price'] = price;
            forms['payway'] = payway;
            $.post(URL+"/build",forms,function(data){
               if(data.status == 1){
                   c.hide();
                   $('.maskbg').remove();
                   if(payway == 1){
                       location.href = encodeURI(SITEURL + '/WxPay?orderid='+data.data);
                       /*c.success(data.info,2000,function(){
                           location.href = "/Info/order";
                       })*/
                   }else{
                       c.success(data.info,2000,function(){
                           location.href = "/Info/order";
                       })
                   }
                   return true;
               }else{
                   c.hide();
                   $('.maskbg').remove();
                   c.error(data.info,2000);
                   return false;
               }
            });
        })


    });
});